package config

import (
	"fmt"

	"github.com/spf13/viper"
)

type KafkaOption struct {
	Kafka *Option `mapstructure:"kafka"`
}

type Option struct {
	// 集群节点
	Brokers []string `mapstructure:"brokers"`
	// kafka主题
	Topic []string `mapstructure:"topic"`
	// 重试次数
	Retry int `mapstructure:"retry"`
	// 重试间隙时长
	RetryMs int `mapstructure:"retry-ms"`
	// sasl用户
	SASLUsername string `mapstructure:"sasl-username"`
	// sasl密码
	SASLPassword string `mapstructure:"sasl-password"`
	// kafka版本
	Version string `mapstructure:"version"`
	// 消费者组id
	GroupID string `mapstructure:"group_id"`
	// 消费者缓冲
	BuffSize int `mapstructure:"buff_size"`
}

func NewOption(filePath string) *Option {
	viper.SetConfigFile(filePath)
	if err := viper.ReadInConfig(); err != nil {
		panic(fmt.Errorf("read in config,err:%s\n", err))
	}
	conf := new(KafkaOption)
	if err := viper.Unmarshal(conf); err != nil {
		panic(fmt.Errorf("unmarshal conf failed, err:%s \n", err))
	}
	return conf.Kafka
}
